from datetime import datetime
import uuid
from pony import orm

from .config import settings

# orm.set_sql_debug(True)


db = orm.Database()
db.bind(
    provider='postgres',
    host=settings.PG_HOST,
    port=settings.PG_PORT,
    database=settings.PG_DATABASE,
    user=settings.PG_USERNAME,
    password=settings.PG_PASSWORD,
)


def generate_uuid():
    return uuid.uuid4().hex

################
# place all entities below
################

# Required, 必须有值的字段, 值不能是null或者''
# Optional, 值可以为null或者''的字段, 字符字段默认为'', 其它字段默认为null

class Hospital(db.Entity):
    _table_ = 'inphile_big_data_hospital'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital = orm.Optional(str)
    img_original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    nhsa_name = orm.Optional(str)
    nhsa_code = orm.Optional(str)
    price = orm.Optional(str)
    medical_insurance_type = orm.Optional(str)
    ratio = orm.Optional(str)
    type = orm.Optional(str)
    quantity_amount = orm.Optional(str)
    remark = orm.Optional(str)
    img_path = orm.Optional(str)
    # 创建时间：设置为标准时间UTC，这样可以节省每次转换所需要的开销，提高性能。
    # creation_time = orm.Required(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True, default=utc_now())
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class City(db.Entity):
    _table_ = 'city'

    id = orm.PrimaryKey(str, default=generate_uuid)
    city = orm.Optional(str)
    original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    medical_insurance_type = orm.Optional(str)
    ratio = orm.Optional(str)
    type = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class Province(db.Entity):
    _table_ = 'province'

    id = orm.PrimaryKey(str, default=generate_uuid)
    province = orm.Optional(str)
    original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    type = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class Country(db.Entity):
    _table_ = 'inphile_big_data_country'

    id = orm.PrimaryKey(str, default=generate_uuid)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    type = orm.Optional(str)


class Word_frequency(db.Entity):
    _table_ = 'word_frequency'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital = orm.Optional(str)
    img_original_name = orm.Optional(str)
    num = orm.Optional(int, default=1, nullable=True)
    type = orm.Optional(str)

    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    last_modified = orm.Optional(datetime, volatile=True)


class Hospital_coding(db.Entity):
    _table_ = 'inphile_big_data_hospital_coding'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital_name_nhsa = orm.Optional(str)
    city = orm.Optional(str)
    province = orm.Optional(str)
    address = orm.Optional(str)
    level = orm.Optional(str)
    hospital_type = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    last_modified = orm.Optional(datetime, volatile=True)
    district = orm.Optional(str)
    addr_id = orm.Optional(str)


class Hospital_mapping(db.Entity):
    _table_ = 'inphile_big_data_hospital_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital_name_seal = orm.Optional(str)
    hospital_name_bill = orm.Optional(str)
    hospital_name_nhsa = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病映射累计报表
class Direct_mapping(db.Entity):
    _table_ = 'inphile_big_data_disease_direct_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    name = orm.Optional(str)
    customer_name = orm.Optional(str)
    assayer = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病
class Code_map(db.Entity):
    _table_ = 'inphile_big_data_disease_normal_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    normal_name = orm.Optional(str)
    normal_code = orm.Optional(str)
    customer_code = orm.Optional(str)
    customer_name = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病
class Party_a_data(db.Entity):
    _table_ = 'inphile_big_data_disease_customer_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    disease_code = orm.Optional(str)
    disease_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    outsource = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病
class Department(db.Entity):
    _table_ = 'inphile_big_data_department_another_name'

    id = orm.PrimaryKey(str, default=generate_uuid)
    standard_name = orm.Optional(str)
    another_name = orm.Optional(str)
    remark = orm.Optional(str)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 中草药
class Inphile_Alg_Chinese_Medicine(db.Entity):
    _table_ = 'inphile_alg_chinese_medicine'

    id = orm.PrimaryKey(str, default=generate_uuid)
    name = orm.Optional(str)
    create_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 联想输入库
class ShuyuInput(db.Entity):
    _table_ = 'inphile_big_data_shuyu_input'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital = orm.Optional(str)
    name = orm.Required(str)
    type = orm.Optional(str)
    num = orm.Required(int, default=0)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    last_modified = orm.Optional(datetime, volatile=True)


# 蓝卡业务联想输入法词库
class Inphile_Alg_Lancare_Input(db.Entity):
    _table_ = 'inphile_big_data_lancare_input'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital = orm.Optional(str)
    name = orm.Required(str)
    type = orm.Optional(str)
    num = orm.Required(int, default=0)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    last_modified = orm.Optional(datetime, volatile=True)

# json\pdf解析结果
class Bill(db.Entity):
    _table_ = 'inphile_alg_bill'

    id = orm.PrimaryKey(str, default=generate_uuid)
    app_name = orm.Optional(str)
    status = orm.Required(int, default=0)
    case_id = orm.Required(str)
    image_id = orm.Required(str, unique=True)
    image_path = orm.Required(str)
    bill_code = orm.Optional(str)
    bill_number = orm.Optional(str)
    bill_check_code = orm.Optional(str)
    bill_date = orm.Optional(str)
    bill_amount = orm.Optional(str)
    bill_payer = orm.Optional(str)
    result = orm.Optional(orm.Json)
    error = orm.Optional(str)
    remark = orm.Optional(str)
    operator = orm.Optional(str)
    operator_expire_time = orm.Optional(datetime, volatile=True)
    operator_submit_time = orm.Optional(datetime, volatile=True)
    create_time = orm.Required(datetime, volatile=True, default=datetime.now())
    end_time = orm.Optional(datetime, volatile=True)
    update_time = orm.Required(datetime, volatile=True, default=datetime.now())
    biz_code = orm.Optional(str)
    callback_url = orm.Optional(str)

# 未带出明细电子票临时爬取库
class TmpCrawlRes(db.Entity):
    _table_ = 'tmp_crawl_res'

    id = orm.PrimaryKey(str, default=generate_uuid)
    im_path = orm.Optional(str)
    czb_path = orm.Optional(str)
    local_site_path = orm.Optional(str)
    crawl_status = orm.Optional(int)
    update_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 医院别称库
class HospitalBiecheng(db.Entity):
    _table_ = 'inphile_big_data_hospital_biecheng'

    id = orm.PrimaryKey(str, default=generate_uuid)
    create_time = orm.Required(datetime, volatile=True, default=datetime.now())
    name = orm.Optional(str)
    another_name = orm.Optional(str)
    version = orm.Optional(str)
    update_time = orm.Required(datetime, volatile=True, default=datetime.now())


class ChineseCitysCode(db.Entity):
    _table_ = 'chinese_citys_code'

    area_id = orm.PrimaryKey(str)
    area_name = orm.Optional(str)


# 药品归一化库
class MedicineNormalize(db.Entity):
    _table_ = 'inphile_big_data_medicine_normalize'

    id = orm.PrimaryKey(str, default=generate_uuid)

    base_code = orm.Optional(str)
    permission_code = orm.Optional(str)
    nhsa_code = orm.Optional(str)
    standard_name = orm.Optional(str)
    standard_principal_component = orm.Optional(str)
    standard_formulation = orm.Optional(str)
    other_name = orm.Optional(str)
    trade_name = orm.Optional(str)
    principal_component = orm.Optional(str)
    formulation = orm.Optional(str)
    sub_name1 = orm.Optional(str)
    sub_name2 = orm.Optional(str)
    sub_name3 = orm.Optional(str)
    specification = orm.Optional(str)
    manufacturer = orm.Optional(str)
    special_symbol = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 药品特殊库
class MedicineNormalizeSpecial(db.Entity):
    _table_ = 'inphile_big_data_medicine_normalize_special'

    id = orm.PrimaryKey(str, default=generate_uuid)

    base_code = orm.Optional(str)
    permission_code = orm.Optional(str)
    nhsa_code = orm.Optional(str)
    standard_name = orm.Optional(str)
    other_name = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 中草药归一化库
class ChineseMedicineNormalize(db.Entity):
    _table_ = 'inphile_big_data_chinese_medicine_normalize'

    id = orm.PrimaryKey(str, default=generate_uuid)

    nhsa_code = orm.Optional(str)
    standard_name = orm.Optional(str)
    other_name = orm.Optional(str)
    medicinalmaterialsname = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 中草药归一化特殊库


# 服务归一化库
class ServiceNormalize(db.Entity):
    _table_ = 'inphile_big_data_service_normalize'

    id = orm.PrimaryKey(str, default=generate_uuid)

    nhsa_code = orm.Optional(str)
    standard_name = orm.Optional(str)
    other_name = orm.Optional(str)
    standard_component = orm.Optional(str)
    synonym_component = orm.Optional(str)

    sub_name1 = orm.Optional(str)
    sub_name2 = orm.Optional(str)
    sub_name3 = orm.Optional(str)
    specification = orm.Optional(str)
    manufacturer = orm.Optional(str)
    special_symbol = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 服务特殊库
class ServiceNormalizeSpecial(db.Entity):
    _table_ = 'inphile_big_data_service_normalize_special'

    id = orm.PrimaryKey(str, default=generate_uuid)

    nhsa_code = orm.Optional(str)
    standard_name = orm.Optional(str)
    other_name = orm.Optional(str)
    remark = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 服务版本库
class ServiceVersion(db.Entity):
    _table_ = 'inphile_big_data_service_version'

    id = orm.PrimaryKey(str, default=generate_uuid)

    version_code = orm.Optional(str)
    version_name = orm.Optional(str)
    data_source = orm.Optional(str)
    type = orm.Optional(str)
    standard_code = orm.Optional(str)
    standard_name = orm.Optional(str)
    remark = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 药、服务版本库
class Version(db.Entity):
    _table_ = 'inphile_big_data_version'

    id = orm.PrimaryKey(str, default=generate_uuid)

    version_code = orm.Optional(str)
    version_name = orm.Optional(str)
    data_source = orm.Optional(str)
    type = orm.Optional(str)
    standard_code = orm.Optional(str)
    standard_name = orm.Optional(str)
    remark = orm.Optional(str)
    classification = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 耗材归一化库
class ConsumableNormalize(db.Entity):
    _table_ = 'inphile_big_data_consumable_normalize'

    id = orm.PrimaryKey(str, default=generate_uuid)
    nhsa_code = orm.Optional(str)

    common_name = orm.Optional(str)
    first_level_name = orm.Optional(str)
    second_level_name = orm.Optional(str)
    third_level_name = orm.Optional(str)
    fourth_level_name = orm.Optional(str)
    fourth_keyword_synonym = orm.Optional(str)
    third_keyword_synonym = orm.Optional(str)
    common_keyword_synonym = orm.Optional(str)

    sub_name1 = orm.Optional(str)
    sub_name2 = orm.Optional(str)
    sub_name3 = orm.Optional(str)
    specification = orm.Optional(str)
    manufacturer = orm.Optional(str)
    special_symbol = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 耗材特殊库
class ConsumableNormalizeSpecial(db.Entity):
    _table_ = 'inphile_big_data_consumable_normalize_special'

    id = orm.PrimaryKey(str, default=generate_uuid)
    nhsa_code = orm.Optional(str)

    first_level_name = orm.Optional(str)
    second_level_name = orm.Optional(str)
    third_level_name = orm.Optional(str)
    fourth_level_name = orm.Optional(str)
    other_name = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 医院网站库
class HospitalWebData(db.Entity):
    _table_ = 'inphile_big_data_web_hospital_data'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    nhsa_name = orm.Optional(str)
    nhsa_code = orm.Optional(str)
    province_code = orm.Optional(str)
    price = orm.Optional(str)
    unit = orm.Optional(str)
    medical_insurance_type = orm.Optional(str)
    ratio = orm.Optional(str)
    type = orm.Optional(str)
    specification = orm.Optional(str)
    manufacturer = orm.Optional(str)
    content = orm.Optional(str)
    except_content = orm.Optional(str)
    remark = orm.Optional(str)
    addr = orm.Optional(str)

    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


################
# place all entities above
################


# all entities connected to the database must be defined before calling generate_mapping()
# if the tables do not already exist, then they will be created
db.generate_mapping(create_tables=True)
# db.generate_mapping()
